package com.smart.controller;

import com.smart.entity.Device;
import com.smart.entity.Result;
import com.smart.service.IDeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 设备 前端控制器
 * </p>
 *
 * @author Cookies
 * @since 2024-05-14
 */
@RestController
@CrossOrigin
@RequestMapping("/device")
@Api(tags = "设备相关接口")
public class DeviceController {
    @Autowired
    private IDeviceService deviceService;

    @PostMapping("/{current}/{size}")
    @ApiOperation(value = "根据检索条件获取设备列表", response = Result.class)
    public Result getDeviceList(
            @RequestBody(required = false) @ApiParam(name = "设备检索条件") Device condition,
            @PathVariable @ApiParam(name = "当前页", required = true) Integer current,
            @PathVariable @ApiParam(name = "每页数量", required = true) Integer size) {
        if (current == null || size == null) {
            return Result.error("参数不能为空");
        }
        return Result.success(deviceService.getDeviceList(condition, current, size));
    }

    @GetMapping("/count")
    @ApiOperation(value = "获取设备数量", response = Result.class)
    public Result getDeviceCount() {
        return Result.success(deviceService.count());
    }

    @PostMapping
    @ApiOperation(value = "添加设备", response = Result.class)
    public Result addDevice(
            @RequestBody @ApiParam(name = "设备信息", required = true) Device device) {
        if (device == null) {
            return Result.error("参数不能为空");
        }
        return Result.success(deviceService.save(device));
    }

    @PutMapping("/{id}")
    @ApiOperation(value = "修改指定设备信息", response = Result.class)
    public Result updateDevice(
            @RequestBody @ApiParam(name = "设备信息", required = true) Device device) {
        if (device == null) {
            return Result.error("参数不能为空");
        }
        return Result.success(deviceService.updateById(device));
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除指定设备", response = Result.class)
    public Result deleteDevice(
            @PathVariable @ApiParam(name = "设备ID", required = true) Integer id) {
        if (id == null) {
            return Result.error("参数不能为空");
        }
        return Result.success(deviceService.removeById(id));
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除设备", response = Result.class)
    public Result deleteDevice(
            @RequestBody @ApiParam(name = "设备ID列表", required = true) List<Integer> ids) {
        if (ids == null) {
            return Result.error("参数不能为空");
        }
        return Result.success(deviceService.removeByIds(ids));
    }

    @GetMapping("/location/{current}/{size}")
    @ApiOperation(value = "获取设备位置", response = Result.class)
    public Result getDeviceLocation(
            @PathVariable @ApiParam(name = "当前页", required = true) Integer current,
            @PathVariable @ApiParam(name = "每页数量", required = true) Integer size) {
        if (current == null || size == null) {
            return Result.error("参数不能为空");
        }
        return Result.success(deviceService.getDeviceLocation(current, size));
    }
}
